package com.nowcoder.community.dao;

import com.nowcoder.community.entity.LoginTicket;
import org.apache.ibatis.annotations.*;

/**
 * @author: caoxinfang
 * @email: 2717176337@qq.com
 * @date: 2022/3/15 11:53
 */
@Mapper
public interface LoginTicketMapper {


    // 添加状态 使用注解实现SQL语句
    @Insert({
            "insert into login_ticket(user_id,ticket,status,expired) ",
            "value(#{userId},#{ticket},#{status},#{expired})"
    })
    // 设置主键自增，并把主键对应为id
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertLoginTicket(LoginTicket loginTicket);

    // 查询状态
    @Select({
            "select id,user_id,ticket,status,expired ",
            "from login_ticket where ticket=#{ticket}"
    })
    LoginTicket selectByTicket(String ticket);

    // 修改状态 使用动态sql的时候加上 <script></script>标签
//    @Update({
//            "update login_ticket set status=#{status} where ticket=#{ticket} ",
//    })
    @Update({
            "<script>",
            "update login_ticket set status=#{status} where ticket=#{ticket} ",
            "<if test=\"ticket!=null\"> ",
            "and 1=1 ",
            "</if>",
            "</script>"
    })
    int updateStatus(String ticket,int status);

}
